package com.starry.module.system.core.logger.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.starry.core.common.domain.vo.PageVo;
import com.starry.module.system.api.logger.domain.qo.SysLogOperationPageQo;
import com.starry.module.system.api.logger.domain.qo.SysLogOperationQo;
import com.starry.module.system.api.logger.domain.vo.SysLogOperationVo;
import com.starry.module.system.core.logger.entity.SysLogOperation;
import com.starry.module.system.core.logger.mapper.SysLogOperationMapper;
import com.starry.module.system.core.logger.service.SysLogOperationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * 操作日志-服务实现
 * @author xiaoke
 */
@Service("sysLogOperationService")
public class SysLogOperationServiceImpl implements SysLogOperationService {

    private final SysLogOperationMapper sysLogOperationMapper;

    public SysLogOperationServiceImpl(SysLogOperationMapper sysLogOperationMapper) {
        this.sysLogOperationMapper = sysLogOperationMapper;
    }

    @Override
    public PageVo<SysLogOperationVo> page(SysLogOperationPageQo pageQo) {
        PageVo<SysLogOperation> page = sysLogOperationMapper.selectPage(pageQo);
        List<SysLogOperationVo> vos = BeanUtil.copyToList(page.getRows(), SysLogOperationVo.class);
        return new PageVo<>(vos, page.getTotal());
    }

    @Transactional
    @Override
    public Long save(SysLogOperationQo sysLogOperationQo) {
        SysLogOperation sysLogOperation = BeanUtil.toBean(sysLogOperationQo, SysLogOperation.class);
        sysLogOperation.setId(null);
        sysLogOperationMapper.insert(sysLogOperation);
        return sysLogOperation.getId();
    }

    @Transactional
    @Override
    public void removeByIds(Set<Long> idList) {
        if (idList.isEmpty()) return;
        sysLogOperationMapper.deleteByIds(idList);
    }
}

